<?php

class menu extends BaseMenu
{
	public function init_menu()
	{
	    //左边栏
	    foreach ($this->rights as $control => $item)
	    {
	        if (isset($item['hide']) || !$this->checkRights($this->act_rights, $control)) continue;
	        if (!empty($item['url'])) {
	            $url = $item['url'];
	        } elseif (!empty($item['items'])) {
	            $url = BASE_URL . $control . '/' . key($item['items']);
	        } else {
	            $url  = '';
	        }
	        $this->_res['left'][$control] = array(
                'text' => $item['text'],
                'url' => $url,
                'active' => (!$this->module && $control == $this->p_controller_id) ? 'class="active"' : '',
                'icon' => $this->_get_icons($control),
	        	'target' => !empty($item['target']) ? $item['target'] : ''
	        );
	        if (!empty($item['items'])) {
	            $this->_res['left'][$control]['items'] = $this->_child_item($item['items'], $control, $control);
	        }
	    }
	    
	    //右边导航栏
	    $this->_res['nav']['index'] = array(
            'url' => BASE_URL,
            //'text' => $this->rights['index']['text']
	        'text' => '控制台'
	    );
        $title = isset($this->rights[$this->p_controller_id]['items'][$this->p_action_id]['text']) ? $this->rights[$this->p_controller_id]['items'][$this->p_action_id]['text'] : '';
        $this->_res['common']['title'] = $title;
		$this->_res['nav']['control'] = isset($this->_res['left'][$this->p_controller_id]['text']) ? $this->_res['left'][$this->p_controller_id]['text'] : '';
        $this->_res['right']['current'] = array(
            'text' => $title,
            'url' => ''
        );
	    return $this->_res;
	}
	
	private function _child_item($nav, $control, $p_control)
	{
	    $data = array();
	    foreach ($nav as $action => $item) {
	    	if (isset($item['hide'])) {
	    		continue;
	    	}
	    	$key = $action;
	    	if (!empty($item['url'])) {
	    		$url = $item['url'];
	    		$url_params = explode('/', $url);
	    		!empty($url_params[1]) && $control = $url_params[1];
	    		!empty($url_params[2]) && $action = $url_params[2];
	    		//list($null, $control, $action) = explode('/', $url);
	    	} elseif (!empty($item['items'])) {
	    		//$url = BASE_URL . $control . '/' . key($item['items']);
	            $url = BASE_URL . $action . '/' . key($item['items']);
	        } else {
	            $url  = BASE_URL . $control . '/' . $action;
	        }
	        if (!$this->checkRights($this->act_rights, $control, $action)) {
	        	continue;
	        }
	        $data[$key] = array(
                'text' => $item['text'],
                'url' => $url,
	            'active' => ($url == Yii::app()->request->url) ? 'class="active"' : '',
                'icon' => $this->_get_icons($p_control),
	        	'target' => !empty($item['target']) ? $item['target'] : ''
	        );
	        if (!empty($item['items'])) {
	            $data[$action]['items'] = $this->_child_item($item['items'], $action, $p_control);
	        }
	    }
	    return $data;
	}
	
	private function _get_icons($key)
	{
	    $res = array(
	        'index' => 'icon-home',
	        'role' => 'icon-grid',
	        'admin' => 'icon-user',
    		'image' => 'icon-image2'
	    );
	    return !empty($res[$key]) ? $res[$key] : 'icon-screen2';
	}
}